package com.ag.oa.core.controller;

import com.ag.oa.core.entity.vo.ProjectVO;
import com.ag.oa.core.entity.vo.UserVO;
import com.ag.oa.core.service.IProjectService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author:   wjc
 * Date:     2021/10/18
 * Description: 任务，控制层
 */
@RestController("projectController")
@RequestMapping(value = {"/project", "/test/project"})
public class ProjectController {

    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private IProjectService projectService;

    @PostMapping("/createProject")
    public void createProject(@RequestBody ProjectVO projectVO) {

        try {
            projectService.createProject(projectVO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("/getProjectManagerFlag")
    public String getProjectManagerFlag(Integer projectId, Integer userId) {

        String flag = "n";
        try {
            flag = projectService.getProjectManagerFlag(projectId, userId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return flag;
    }

    @GetMapping("/findProjectList")
    public List<ProjectVO> findProjectList(Integer projectStatus, Integer projectUserId, Integer projectId) {

        List<ProjectVO> list = new ArrayList<>();
        try {
            list = projectService.findProjectList(projectStatus, projectUserId, projectId);

            ProjectVO projectVO = projectService.getProjectById(0); // 默认系统项目
            if (projectVO!=null) {
                list.add(0, projectVO);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    @GetMapping("/getProjectById")
    public ProjectVO getProjectById(Integer projectId) {

        ProjectVO vo = new ProjectVO();
        try {
            vo = projectService.getProjectById(projectId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return vo;
    }

    /**
     * 获取我的项目
     */
    @GetMapping("/myProjects")
    public String getMyProjects(Integer userId, Integer page, Integer pageSize) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            List<Map> list = projectService.getAllMyProjectsData(userId, page, pageSize);
            res.put("projectArr", list);
        } catch (Exception e) {
            code = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toString();
    }

    @PostMapping("/updateProjectStatus")
    public void updateProjectStatus(@RequestBody ProjectVO vo) {

        logger.info("--修改合作社状态: "+ vo.toString());
        try {
            projectService.updateProjectStatus(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @PostMapping("/updateProjectDesc")
    public void updateProjectDesc(@RequestBody ProjectVO vo) {

        try {
            projectService.updateProjectDesc(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @PostMapping("/updateConstitution")
    public void updateConstitution(@RequestBody ProjectVO vo) {

        try {
            projectService.updateConstitution(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("/findProjectUserById")
    public List<ProjectVO> findProjectUserById(Integer projectId) {

        List<ProjectVO> list = new ArrayList<>();
        try {
            list = projectService.findProjectUserById(projectId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    @PostMapping("/deleteProjectUserById/{projectId}/{userId}")
    public void deleteProjectUserById(@PathVariable("projectId") Integer projectId
            , @PathVariable("userId") Integer userId) {

        try {
            projectService.deleteProjectUserById(projectId, userId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("/getProjectUserCheckDataList")
    public List<UserVO> getProjectUserCheckDataList(String username, Integer projectId, Integer limitCount) {

        List<UserVO> list = new ArrayList<>();
        try {
            list = projectService.getProjectUserCheckDataList(username, projectId, limitCount);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    @PostMapping("/createProjectUsers")
    public void createProjectUsers(@RequestBody ProjectVO projectVO) {

        try {
            projectService.createProjectUsers(projectVO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 获取合作社的所有社员信息
     */
    @GetMapping("/getProjectMembers")
    public String getMembers(Integer projectId) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            List<Map> memberList = projectService.findProjectAllMembers(projectId);
            res.put("memberArr", memberList);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 分页获取合作社可添加的社员列表信息
     */
    @GetMapping("/addable/members")
    public String getAddableMembers(Integer projectId, Integer userId, Integer page, Integer pageSize
            , String username) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            List<Map> userArrList = projectService.getProjectAddableMembers(projectId, userId, page, pageSize
                    , username);
            res.put("userArr", userArrList);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 添加保存合作社社员
     * @param checkedUserStr 用户 id 逗号分隔
     */
    @GetMapping ("/saveProjectMembers")
    public String saveMembers(Integer projectId, Integer userId, String checkedUserStr) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            projectService.saveProjectMemberUsers(projectId, userId, checkedUserStr);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 删除合作社成员
     */
    @GetMapping ("/delProjectMember")
    public String delMembers(Integer projectId, Integer userId) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            projectService.delProjectMemberUser(projectId, userId);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 保存添加项目积分记录
     * @param userId 被添加人
     * @param addUserId 积分记录添加者
     */
    @GetMapping ("/saveProjectScore")
    public String saveMemberScore(Integer projectId, Integer userId, Integer addUserId, Integer score
            , String description) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            projectService.saveProjectMemberScore(projectId, userId, addUserId, score, description);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 点击个人详情页 '积分', 获取所参与的合作社获得的积分
     */
    @GetMapping ("/projectScoreList")
    public String getMemberScores(Integer userId) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            List<Map> scoreList = projectService.getUserAllProjectScoreList(userId);
            res.put("projectScoreArr", scoreList);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 获取社员合作社(项目) 积分记录
     */
    @GetMapping ("/projectScoreDetail")
    public String getScoreDetail(Integer projectId, Integer userId) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            List<Map> scoreArrList = projectService.getUserProjectScoreDetail(projectId, userId);
            res.put("scoreArrList", scoreArrList);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

    /**
     * 获取社员合作社(项目) 积分记录
     */
    @GetMapping ("/projectScoreInfo")
    public String getScoreInfo(Integer scoreId) {

        JSONObject res = new JSONObject();
        int resCode = 200;
        try {
            Map<String, Object> info = projectService.getProjectScoreInfo(scoreId);
            res.put("scoreInfo", info);
        } catch (Exception e) {
            resCode = 500;
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", resCode);
        return res.toString();
    }

}
